package com.example.dairenkun.neufoodshop.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;

import com.example.dairenkun.neufoodshop.bean.EditUser;
import com.example.dairenkun.neufoodshop.bean.User;
import com.example.dairenkun.neufoodshop.service.UserService;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class EditUserActivity extends AppCompatActivity {

    private TextView tv_username;
    private TextView tv_phone;
    private TextView tv_address;
    private TextView tv_pass;

    private EditText et_username;
    private EditText et_phone;
    private EditText et_address;
    private EditText et_pass;

    private Button btn_edit;

    private ImageView userpic;
    int  userid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_user);
        initViews();
        initEvents();
        Intent intent =getIntent();
        userid=Integer.valueOf(intent.getStringExtra("user_id"));
        et_username.setText(intent.getStringExtra("username"));
        et_phone.setText(intent.getStringExtra("userphone"));
        et_address.setText(intent.getStringExtra("useraddress"));
       //et_pass.setText(intent.getStringExtra("userpass"));
       /* //获取用户信息
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://60.205.189.39/ ")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserService testService=retrofit.create(UserService.class);
        Call<User> call=testService.getUserInfo(userid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, retrofit2.Response<User> response)
            {
                User user=response.body();
                et_username.setText(user.getUsername());
                et_phone.setText(user.getMobilenum());
                et_address.setText(user.getAddress());
                et_pass.setText(user.getUserpass());

            }
            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });*/

    }
    public void initEvents(){

        //点击修改信息
        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit=new Retrofit.Builder()
                        .baseUrl("http://60.205.189.39/ ")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                UserService testService=retrofit.create(UserService.class);
                Call<EditUser> call=testService.updateUserInfo(userid,et_username.getText().toString(),
                        et_pass.getText().toString(),et_phone.getText().toString(),et_address.getText().toString());
                call.enqueue(new Callback<EditUser>() {
                    @Override
                    public void onResponse(Call<EditUser> call, retrofit2.Response<EditUser> response)
                    {
                        EditUser res=response.body();
                        String result =res.getSuccess();

                        if("0".equals(result))
                        {
                            //失败处理
                            Toast.makeText(EditUserActivity.this, "修改失败！", Toast.LENGTH_SHORT).show();
                        } else
                        {
                            //登录成功处理
                            Toast.makeText(EditUserActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();


                        }



                    }
                    @Override
                    public void onFailure(Call<EditUser> call, Throwable t) {

                    }
                });
            }
        });

    }
    public void initViews(){
        tv_username=(TextView)findViewById(R.id.edituser_text_name);
        tv_phone=(TextView)findViewById(R.id.edituser_text_phone);
        tv_address=(TextView)findViewById(R.id.edituser_text_address);
        tv_pass=(TextView)findViewById(R.id.edituser_text_pass);

        et_username=(EditText) findViewById(R.id.edituser_edit_name);
        et_phone=(EditText)findViewById(R.id.edituser_edit_phone);
        et_address=(EditText)findViewById(R.id.edituser_edit_address);
        et_pass=(EditText)findViewById(R.id.edituser_edit_pass) ;

        btn_edit=(Button)findViewById(R.id.btn_edituser_confirm);
        userpic=(ImageView)findViewById(R.id.edituser_pic);

    }
}
